//
//  SetViewController.m
//  ECO
//
//  Created by 谷银行 on 2019/9/23.
//  Copyright © 2019 谷银行. All rights reserved.
//

#import "SetViewController.h"
#import "CompileViewController.h"
#import "AppDelegate.h"
#import "PhotoCell.h"
#import "DisturbingCell.h"
#import "ClearCell.h"
#import "ShortcutCell.h"
@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource,ShortcutCellDelegate,ClearCellDelegate,DisturbingCellDelegate>
@property(nonatomic,strong)UITableView *ojtable;
@end

@implementation SetViewController
- (UITableView *)ojtable{
    if (!_ojtable) {
        _ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _ojtable.delegate = self;
        _ojtable.dataSource = self;
        _ojtable.scrollEnabled = NO;
    }
    return _ojtable;
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]init];
    label.text = @"设置";
    label.font = [UIFont systemFontOfSize:18 * autoSizeScaleX weight:1.5 * autoSizeScaleX];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:label];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.view addSubview:self.ojtable];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    _ojtable.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (indexPath.row == 0) {
        PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[PhotoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *imgV =[user objectForKey:@"iconUrl"];
        [cell.imgV sd_setImageWithURL:[NSURL URLWithString:imgV] placeholderImage:[UIImage imageNamed:@"头像"]];
        cell.Name.text = [user objectForKey:@"nickname"];
        return cell;
    }else if(indexPath.row == 1){
        DisturbingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[DisturbingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        // 88
        return cell;
    }else if (indexPath.row == 2){
        ClearCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[ClearCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.delegate = self;
        cell.Title.text = @"清理缓存";
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.DetailText.text = [NSString stringWithFormat:@"%.2fk",[KyClearCache KyfloatSize]];
        return cell;
    }else{
        ShortcutCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[ShortcutCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.delegate = self;
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 88 * autoSizeScaleX;
    }else{
        return 47 * autoSizeScaleX;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 142 * autoSizeScaleX;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
//    view.backgroundColor = [UIColor redColor];
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMakeCustom(15, 49, 320 - 30, 44)];
    btn.backgroundColor = [UIColor colorWithHexString:@"#00DAAF"];
    [btn setTitle:@"切换账号" forState:UIControlStateNormal];
    btn.layer.cornerRadius = 22 * autoSizeScaleX;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(FootBtn:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
}
- (void)FootBtn:(UIButton *)send{
    NSLog(@"切换账号");
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"rytoken"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"iconUrl"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"nickname"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"token"];
    //  断开与融云服务器的连接，不接收远程推送
    [[RCIMClient sharedRCIMClient]disconnect:NO];
    RegisterViewController * modal = [[RegisterViewController alloc] init];
    //把当前控制器作为背景
    self.definesPresentationContext = YES;
    //设置模态视图弹出样式
    modal.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:modal animated:YES completion:nil];
}
- (void)ShortcutCellClick{
    CompileViewController *Compile = [CompileViewController new];
    // 跳转后更改返回按钮文字
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc] init];
    returnButtonItem.title = @"";
    returnButtonItem.tintColor = [UIColor blackColor];
    self.navigationItem.backBarButtonItem = returnButtonItem;
    [self.navigationController pushViewController:Compile animated:YES];
}
// 免打扰代理
- (void)DisturbingCellClick{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}
// 清理缓存代理
- (void)ClearCellClick:(UIButton *)send{
    NSLog(@"清理缓存");
    [KyClearCache KyClear:^{
        
    }];
    [self.ojtable reloadData];
}

@end
